package com.toobigtofail.drum
{
	import flash.events.KeyboardEvent;
	
	import net.flashpunk.FP;
	
	public class DrumListener {
		private var _enabled:Boolean = false;
		private var _drumsHit:Array = new Array;
		private var _allowedInput:Array = new Array;
		
		public function start():void {
			if (!_enabled && FP.stage) {
				FP.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
				_enabled = true;
			}
		}
		
		public function stop():void {
			if (_enabled && FP.stage) {
				FP.stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
				_enabled = false;
			}
		}
		
		/**
		 * Take in a list of notes as defined in Notes, translate them to actual input keys
		 * (for which the mappings should already be settup in Notes) and add them to
		 * the allowed input list.
		 */
		public function setDrums(notes:Vector.<int>):Array {
			_allowedInput = new Array;
			for each (var note:* in notes) {
				_allowedInput.push(Notes.getKey(note));
			}
			return _allowedInput;
		}
		
		public function get allowedDrums():Array {
			return _allowedInput;
		}
		
		public function getDrumsHit():Array {
			var ret:Array = _drumsHit.concat(); // clone the array
			_drumsHit = new Array; // clear the drums hit
			return ret;
		}
		
		private function onKeyDown(e:KeyboardEvent=null):void {
			for each (var key:int in _allowedInput) {
				if (key == e.keyCode) {
					_drumsHit.push(key);
				}
			}
		}
	}
}